import sys
from  PyQt5.QtWidgets import *
from PyQt5 import QtCore
from PyQt5.QtGui import QColor,QBrush
class TableDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.initUi()
    def initUi(self):
        self.setWindowTitle("QtableWidget案例")
        self.resize(400,300)
        conLayout=QHBoxLayout()
        tablewidget=QTableWidget()
        tablewidget.setRowCount(40)
        tablewidget.setColumnCount(4)
        conLayout.addWidget(tablewidget)
        for i in range(40):
            for j in  range(4):
                itemContent='(%d,%d)'%(i,j)
                tablewidget.setItem(i,j,QTableWidgetItem(itemContent))
        self.setLayout(conLayout)
        #遍历表格查找对应的选项
        text="(10,1)"
        items=tablewidget.findItems(text,QtCore.Qt.MatchExactly)
        item=items[0]

        #选中单元格
        item.setForeground(QBrush(QColor(255,0,0)))
        row=item.row()
        tablewidget.verticalScrollBar().setSliderPosition(row)
if __name__ == '__main__':
    app=QApplication(sys.argv)
    table=TableDemo()
    table.show()
    sys.exit(app.exec_())